package com.example.exception;

import com.example.vo.resp.Result;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result methodArgumentNotValidException(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        Map<String, String> r = new HashMap<>();
        bindingResult.getFieldErrors().forEach(f -> {
            r.put(f.getField(), f.getDefaultMessage());
        });
        return Result.FAILED(r);
    }

    @ExceptionHandler(BizException.class)
    public Result bizException(BizException e) {
        return Result.FAILED(e.getInfo());
    }

//    @ExceptionHandler(InvalidFormatException.class)
//    public Result invalidFormatException(InvalidFormatException e) {
//        Map<String, String> r = new HashMap<>();
//        return Result.FAILED(r);
//    }

}
